package com.fs.groupSeven.controller;

import com.fs.groupSeven.pojo.College;
import com.fs.groupSeven.pojo.Subject;
import com.fs.groupSeven.pojo.Teacher;
import com.fs.groupSeven.service.SubjectService;
import com.fs.groupSeven.service.TeacherService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author： mjx
 * @createDate: 2020/10/9
 * @version: 1.0
 */
//注明这是一个IOC容器管理的bean
@Controller
@RequestMapping("/teacher")
public class TeacherManageController {

    @Autowired//依赖注入 自动装配subjectService
    private TeacherService teacherService;

    @RequestMapping("/getTeacher")
    public String  role(ModelMap map){
        return "/subjectManageMjx/teacherManage";
        /*使用视图解析器后，返回一个jsp的名字即可，前后缀已经由视图解析器帮我们完成*/
    }

    @RequestMapping("/getTeacherListByPage")
    @ResponseBody//告诉框架返回的格式为json字符串类型，放在响应体里面
    public Map<String,Object> getListByPage(Integer currentPage,
                                            Integer pageSize){

        PageInfo<Teacher> pageInfo = teacherService.getTeachersByPage(currentPage, pageSize);
//        System.out.println("获取得总数为："+pageInfo.getTotal());

        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取课程数据成功");
        result.put("teacherList",pageInfo.getList());//返回查询出来的分页数据
        result.put("sum",pageInfo.getTotal());
        result.put("totalPage",pageInfo.getPages());//返回当前总共有多少页
        return result;
    }

    @RequestMapping("/getOneTeacher")
    @ResponseBody
    public Map<String,Object> getOneTeacher(Integer teacherId){
        Map<String,Object> result = new HashMap<>();
        Teacher teacher = teacherService.getOneByTeacherId(teacherId);
//        System.out.println("获取到院级id："+subject.getCollegeId());
//        System.out.println("获取到subject："+subject);
        if(teacher == null){
            result.put("code",-1);
            result.put("message","获取课程数据失败");
        }else{
            result.put("code",0);
            result.put("message","获取课程数据成功");
            result.put("teacher",teacher);
        }
        return result;
    }
    //获得学院们
    @RequestMapping("/getColleges")
    @ResponseBody//告诉框架返回的格式为json字符串类型，放在响应体里面
    public  Map<String,Object>  getSubjectList(){
        List<College> collegeList = teacherService.getCollegelist();
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取用户数据成功");
        result.put("collegeList",collegeList);
        return result;
    }
    //修改老师信息
    @RequestMapping("/editTeacher")
    @ResponseBody
    public Map<String,Object> editTeacher(@RequestBody Teacher teacher){
//        System.out.println("编辑课程信息进入控制层subjectId"+subjectId);
        Map<String,Object> result = new HashMap<>();
//        System.out.println("获取到老师："+teacher);
        boolean modified;
        if(teacher.getTeacherId()>0) {
            modified = teacherService.modifyOneTeacher(teacher);
        }else {
            modified = teacherService.addOneTeacher(teacher);

        }

        if(!modified){
            result.put("code",-1);
            result.put("message","编辑课程数据失败");
        }else{
            result.put("code",0);
            result.put("message","编辑课程数据成功");
            result.put("teacher",teacher);
        }
        return result;
    }

    @RequestMapping("/deleteOneByTeacher")
    @ResponseBody
    public Map<String,Object> deleteOneByTeacher(Teacher teacher){
        Map<String,Object> result = new HashMap<>();
        boolean deleted = teacherService.removeOneTeacher(teacher);
        if(!deleted){
            result.put("code",-1);
            result.put("message","删除失败");
        }else{
            result.put("code",0);
            result.put("message","删除成功");
        }
        return result;
    }

    @RequestMapping("/getListByPageAndTeacherName")
    @ResponseBody//告诉框架返回的格式为json字符串类型，放在响应体里面
    public Map<String,Object> getListByPageAndSubjectName(Integer currentPage,
                                                          Integer pageSize,String teacherName){
        System.out.println("获取到模糊查询得老师姓名"+teacherName);

        PageInfo<Teacher> pageInfo = teacherService.getListByPageAndTeacherName(currentPage, pageSize,teacherName);

        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取课程数据成功");
        result.put("sum",pageInfo.getTotal());
        result.put("teacherList",pageInfo.getList());//返回查询出来的分页数据

        result.put("totalPage",1);//返回当前总共有多少页
        return result;
    }
}
